
#include "common.h"
#include "VL53L0X.h"






/*******************************************************************************
 * Variables
 ******************************************************************************/
lpi2c_rtos_handle_t rtos_lpi2c_config;
lpi2c_master_handle_t g_m_handle;
lpi2c_master_config_t masterConfig;
lpi2c_master_transfer_t VL53L0X_Transfer_Config;

volatile bool g_MasterCompletionFlag = false;

status_t reVal = kStatus_Fail;

uint8_t g_master_txBuff[1];
uint8_t val[1];
uint8_t Rcv_flag[1];
uint8_t gbuf[16];
uint16_t count[3];

/*******************************************************************************
 * Code
 ******************************************************************************/

static void lpi2c_master_callback(LPI2C_Type *base, lpi2c_master_handle_t *handle, status_t status, void *userData)
{
    /* Signal transfer success when received success status. */
    if (status == kStatus_Success)
    {
        g_MasterCompletionFlag = true;
    }
}

uint16_t makeuint16(int lsb, int msb)
{
    return ((msb & 0xFF) << 8) | (lsb & 0xFF);
}

uint8_t VL53L0X_Init(void)
{
	g_master_txBuff[0] = 0x01;

    rtos_lpi2c_config.drv_handle = g_m_handle;

    VL53L0X_Transfer_Config.flags = 0;
    VL53L0X_Transfer_Config.slaveAddress = VL53L0X_Add;
    VL53L0X_Transfer_Config.direction = kLPI2C_Write;
    VL53L0X_Transfer_Config.subaddress = 0x00;//(uint32_t)0x00U;
    VL53L0X_Transfer_Config.subaddressSize = 1;
    VL53L0X_Transfer_Config.data = g_master_txBuff;
    VL53L0X_Transfer_Config.dataSize = 1;
    VL53L0X_Transfer_Config.flags = kLPI2C_TransferDefaultFlag;

    LPI2C_MasterGetDefaultConfig(&masterConfig);

    /* Change the default baudrate configuration */
    masterConfig.baudRate_Hz = I2C_BAUDRATE;
    // LPI2C_MasterInit(EXAMPLE_I2C_MASTER, &masterConfig, LPI2C_MASTER_CLOCK_FREQUENCY);
    if(LPI2C_RTOS_Init(&rtos_lpi2c_config, EXAMPLE_I2C_MASTER, &masterConfig, LPI2C_MASTER_CLOCK_FREQUENCY) != kStatus_Success)
    	return 1;

    NVIC_SetPriorityGrouping(0);
    NVIC_SetPriority(LPI2C1_IRQn,5);

	return 0;
}

uint8_t VL53L0X_Write_Byte(void)
{
	reVal = kStatus_Fail;

	VL53L0X_Transfer_Config.subaddress = 0x00;
    g_master_txBuff[0] = 0x01;

	reVal = LPI2C_RTOS_Transfer(&rtos_lpi2c_config, &VL53L0X_Transfer_Config);

	if(reVal != kStatus_Success)
		return 1;
	else
		return 0;
}

uint8_t VL53L0X_Read_Byte(void)
{
	uint16_t cnt = 0;

	Rcv_flag[0] = 0;
	g_master_txBuff[0] = VL53L0X_REG_RESULT_RANGE_STATUS;

	while(cnt < 100)
	{
		TaskDelay_ms(10);

		LPI2C_MasterStart(EXAMPLE_I2C_MASTER, VL53L0X_Add, kLPI2C_Write);
		LPI2C_MasterSend(EXAMPLE_I2C_MASTER, g_master_txBuff, 1);
		LPI2C_MasterStop(EXAMPLE_I2C_MASTER);
		LPI2C_MasterStart(EXAMPLE_I2C_MASTER, VL53L0X_Add, kLPI2C_Read);
		LPI2C_MasterReceive(EXAMPLE_I2C_MASTER, Rcv_flag, 1);
		LPI2C_MasterStop(EXAMPLE_I2C_MASTER);

		if( Rcv_flag[0] & 0x01) break;
		cnt++;
	}

	if(Rcv_flag[0] != 0)
        return 0;
    else
        return 1;
}

uint8_t VL53L0X_Read_Buf(void)
{
    if(LPI2C_MasterStart(EXAMPLE_I2C_MASTER, VL53L0X_Add, kLPI2C_Write) == kStatus_Fail)
    {
        LPI2C_MasterStop(EXAMPLE_I2C_MASTER);
        return 1;
    }
    g_master_txBuff[0] = 0x14;
    LPI2C_MasterSend(EXAMPLE_I2C_MASTER, g_master_txBuff, 1);
    LPI2C_MasterStop(EXAMPLE_I2C_MASTER);
    LPI2C_MasterStart(EXAMPLE_I2C_MASTER, VL53L0X_Add, kLPI2C_Read);
    LPI2C_MasterReceive(EXAMPLE_I2C_MASTER, gbuf, 12);
    LPI2C_MasterStop(EXAMPLE_I2C_MASTER);

    return 0;
}

